package com.tx.admin.controller.security;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tx.admin.controller.po.LoginPo;
import com.tx.admin.controller.vo.SuperVo;
import com.tx.admin.controller.vo.VuexVo;
import com.tx.admin.security.service.SecurityService;
import com.tx.admin.util.web.RespUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/security")
@Api(tags = "安全中心")
public class SecurityController {

	@Autowired
	private SecurityService securityService;

	//创建邮件验证码
	  @ApiOperation("发送邮件")
	@PostMapping("/createVerifyCodeByMail")
	public  SuperVo createVerifyCodeByMail(@RequestBody LoginPo loginParams) {
    	 securityService.createVerifyCodeByMail(loginParams);
    	 return SuperVo.newVo("验证码已发送");
	}
     
     //根据邮件验证码修改密码
	  @ApiOperation("根据邮件验证码修改密码")
	@PostMapping("/updatePasswordByMail")
	public SuperVo updatePasswordByMail (@RequestBody LoginPo loginParams) {
    	 securityService.updatePasswordByMail(loginParams);
    	 return SuperVo.newVo("密码修改成功");
	}

	  @ApiOperation("登陆")
	@PostMapping("/login")
	public SuperVo login(@Validated  @RequestBody LoginPo params) {
    	 securityService.login(params);
    	 return SuperVo.newVo("登陆成功");
     }
     
	  @ApiOperation("注销")
	//注销
	@PostMapping("/logout")
	public SuperVo logout() {
		securityService.logout();
		 return SuperVo.newVo("注销成功");
	}
	
	  @ApiOperation("创建验证码")
	@GetMapping("/createVerifyCode")
	//创建验证码
	public void  createVerifyCode(HttpServletResponse resp ) {
		RespUtil.writeImage(resp, securityService.createVerifyCode());
	}

	  @ApiOperation("切换身份")
	//切换身份
	@PostMapping("/runUser")
	public SuperVo runUser(@RequestBody String userName) {
		 this.securityService.runUser(userName);
		 return SuperVo.newVo("切换身份成功");
	}
	  @ApiOperation("刷新权限")
	@PostMapping("/flushRole")
	 //刷新角色
	public SuperVo flushRole() {
		 securityService.flushRole();
		 return SuperVo.newVo("刷新权限成功");
	}
	  
	@ApiOperation("获取用户数据源")
	@PostMapping("/vuex")
	 public VuexVo vuex() {
		 VuexVo vo = new VuexVo();
		 vo.setUser(securityService.getUser());
		 vo.setDeparment(securityService.getDepartment());
		 vo.setRoles(securityService.getRoleAll());
		 vo.setDataPermissions(securityService.getDataPermission());
		 vo.setDataFunctions(securityService.getDataFunction());
		 return vo;
	 }
	
}
